﻿/* Copyright (c) 2010 EPiServer AB
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Web.UI;
using EPiServer.Core;
using EPiServer.Web.WebControls;

namespace EPiServer.Municipality.Web.UI.WebControls {
    /// <summary>
    /// Summary description for MenuControl.
    /// </summary>
    /// <remarks>
    /// </remarks>
    public class MenuControl : MenuList {
        /* *******************************************************************
         *  Properties 
         * *******************************************************************/
        #region private PageData StartPage
        /// <summary>
        /// Gets the StartPage of the MenuControl
        /// </summary>
        /// <value></value>
        private PageData StartPage {
            get {
                if (String.IsNullOrEmpty(PageLinkProperty))
                    return GetPage(PageReference.StartPage);
                return GetPage((PageReference)CurrentPage[PageLinkProperty]);
            }
        }

        #endregion
        #region public ITemplate HomeTemplate

        /// <summary>
        /// Get/Sets the HomeTemplate of the MenuControl
        /// </summary>
        /// <value></value>
        [TemplateContainer(typeof (PageTemplateContainer))]
        public ITemplate HomeTemplate { get; set; }

        #endregion
        #region public ITemplate SelectedHomeTemplate

        /// <summary>
        /// Get/Sets the SelectedHomeTemplate of the MenuControl
        /// </summary>
        /// <value></value>
        [TemplateContainer(typeof (PageTemplateContainer))]
        public ITemplate SelectedHomeTemplate { get; set; }

        #endregion
        #region public ITemplate LastTemplate

        /// <summary>
        /// Get/Sets the LastTemplate of the MenuControl
        /// </summary>
        /// <value></value>
        [TemplateContainer(typeof(PageTemplateContainer))]
        public ITemplate LastTemplate { get; set; }

        #endregion
        #region public ITemplate SelectedLastTemplate

        /// <summary>
        /// Get/Sets the SelectedLastTemplate of the MenuControl
        /// </summary>
        /// <value></value>
        [TemplateContainer(typeof(PageTemplateContainer))]
        public ITemplate SelectedLastTemplate { get; set; }

        #endregion
        #region public ITemplate ExpandedTemplate

        /// <summary>
        /// Get/Sets the ExpandedTemplate of the MenuControl
        /// </summary>
        /// <value></value>
        [TemplateContainer(typeof(PageTemplateContainer))]
        public ITemplate ExpandedTemplate { get; set; }

        #endregion
        #region public ITemplate ExpandedLastTemplate

        /// <summary>
        /// Get/Sets the ExpandedLastTemplate of the MenuControl
        /// </summary>
        /// <value></value>
        [TemplateContainer(typeof(PageTemplateContainer))]
        public ITemplate ExpandedLastTemplate { get; set; }

        #endregion
        /* *******************************************************************
         *  Methods 
         * *******************************************************************/
        #region protected override void CreateChildControls()

        /// <summary>
        /// 
        /// </summary>
        protected override void CreateChildControls() {
            PageHierarchicalEnumerable enumerable = PageLoader.HierarchicalSelect(string.Empty);
            if (enumerable.Pages.Count != 0) {
                PageData pageData = GetPage(RootLink);
                CreateTemplateControl(pageData, HeaderTemplate, 0, false);
                bool flag = true;
                if(HomeTemplate != null && SelectedHomeTemplate != null) {
                    if (StartPage.PageLink.CompareToIgnoreWorkID(CurrentPage.PageLink) ||
                        !PageReference.IsNullOrEmpty(OpenTopPage) && StartPage.PageLink.CompareToIgnoreWorkID(OpenTopPage)) {
                        CreateTemplateControl(StartPage, SelectedHomeTemplate, 1, true);
                    }
                    else {
                        CreateTemplateControl(StartPage, HomeTemplate, 1, true);
                    }
                }
                int count = 0;
                foreach (PageHierarchyData data in enumerable) {
                    count++;
                    if (!flag)
                        CreateTemplateControl(null, SeparatorTemplate, 1, false);
                    else
                        flag = false;
                    PageData page = data.Page;
                
                    PageTemplateContainer child = new PageTemplateContainer(page, 1, data.HasChildren);
                    if (page.PageLink.CompareToIgnoreWorkID(CurrentPage.PageLink)) {
                        if (count == enumerable.Pages.Count)
                            child.InstantiateWithFallback(new[] { SelectedLastTemplate, SelectedTemplate, ItemTemplate});
                        else
                            child.InstantiateWithFallback(new[] { SelectedTemplate, ItemTemplate });    
                    } 
                    else if (!PageReference.IsNullOrEmpty(OpenTopPage) && page.PageLink.CompareToIgnoreWorkID(OpenTopPage)) {
                        if (count == enumerable.Pages.Count)
                            child.InstantiateWithFallback(new[] { ExpandedLastTemplate, SelectedLastTemplate, SelectedTemplate, ItemTemplate });
                        else
                            child.InstantiateWithFallback(new[] { ExpandedTemplate, SelectedTemplate, ItemTemplate });    
                    }
                    else {
                        if (count == enumerable.Pages.Count)
                            child.InstantiateWithFallback(new[] { LastTemplate, ItemTemplate });
                        else
                            child.InstantiateWithFallback(new[] {ItemTemplate});
                    }
                    Controls.Add(child);
                }
                CreateTemplateControl(pageData, FooterTemplate, 0, false);
            }
        }
        #endregion
        /* *******************************************************************
         *  Event methods 
         * *******************************************************************/
    }
}